package cn.iocoder.yudao.module.testequipmentuse.dal.dataobject.testequipmentuse;

import cn.iocoder.yudao.module.testequipmentuse.controller.admin.testequipmentuse.vo.TestEquipmentUsePageReqVO;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class TestEquipmentUseSqlProvider {

    public String selectPageWithEquipmentInfoByStatus(Map<String, Object> params) {
        TestEquipmentUsePageReqVO query = (TestEquipmentUsePageReqVO) params.get("query");
        Integer status = (Integer) params.get("status");

        SQL sql = new SQL()
                .SELECT("tu.id, te.eq_name as equipmentName, te.model as equipmentModel, " +
                        "te.storage_location as storageLocation, tu.creator, tu.create_time as createTime, tu.operation_type as status")
                .FROM("sys_test_equipment_use tu")
                .LEFT_OUTER_JOIN("sys_test_equipment te ON tu.eq_id = te.id")
                .WHERE("tu.operation_type = #{status}");

        // 添加筛选条件
        if (query != null) {
            if (query.getEqId() != null) {
                sql.WHERE("tu.eq_id = #{query.eqId}");
            }
            if (query.getOperationType() != null) {
                sql.WHERE("tu.operation_type = #{query.operationType}");
            }
            if (query.getEqName() != null && !query.getEqName().isEmpty()) {
                sql.WHERE("te.eq_name LIKE CONCAT('%', #{query.eqName}, '%')");
            }
            if (query.getModel() != null && !query.getModel().isEmpty()) {
                sql.WHERE("te.model LIKE CONCAT('%', #{query.model}, '%')");
            }
        }

        // 添加排序
        sql.ORDER_BY("tu.create_time DESC");

        return sql.toString();
    }
}